<template>
  <div class="">
    <div class="searchBar">
      <el-form label-width="100px">
        <div style="display:flex;">
          <el-form-item label="订单编号:">
            <el-input size="small" v-model="searchValue.orderNumber" placeholder请输入订单编号></el-input>
          </el-form-item>
          <el-form-item label="订单时间:">
            <el-date-picker v-model="searchTimeRange" type="datetimerange" range-separator="至" start-placeholder="开始日期"
              end-placeholder="结束日期" size="small">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="支付类型:">
            <el-select size="small" v-model="searchValue.payType" placeholder="请选择">
              <el-option v-for="item in payTypeOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            </el-date-picker>
          </el-form-item>
          <br>
          <!--          <el-form-item label="充值金额:">
            <el-input size='small' v-model="searchValue.amount" placeholder='请输入充值金额'></el-input> -->
          </el-form-item>
          <el-form-item label="用户昵称:">
            <el-input size='small' v-model="searchValue.nickName" placeholder='请输入用户昵称'></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div style="margin:0 auto;width:250px;">
        <el-button type="primary" class="el-icon-search" size="medium" @click="handleSearch(1)">搜索</el-button>
        <el-button class="el-icon-delete" size="medium" style="margin:20px 0 0 10px;" @click="handleSearch(0)">清空
        </el-button>
      </div>
    </div>


    <avue-crud ref="crud" :page="page" :data="dataList" :table-loading="dataListLoading" :option="tableOption"
      @on-load="loadData">

      <template slot='menu' slot-scope="info">
        <el-button type='primary' size='mini' @click='openMemberRechargeHistory(info)'>充值记录</el-button>
      </template>

      <template slot='enterAccountStatus' slot-scope='{row}'>
        <el-tag v-if="row.enterAccountStatus==2" type='danger'>未入账</el-tag>
        <el-popover v-if="row.enterAccountStatus == 1" placement="top-start" title="标题" width="200" trigger="hover" content="这是一段内容,这是一段内容,这是一段内容,这是一段内容。">
          <el-tag>入账</el-tag>
        </el-popover>
      </template>

      <template slot="service_fee" slot-scope="{row}">
        <div>{{-row.service_fee}}</div>
      </template>

      <template slot="rebateMoney" slot-scope="{row}">
        <div>{{-row.rebateMoney}}</div>
      </template>

    </avue-crud>

    <el-dialog v-if="showDialog" title="充值记录" :visible.sync="dialog.show" width="60%" :before-close="handleClose">
      <avue-crud ref="dialog-crud" :page="dialog.page" :data="dialog.dataList" :table-loading="dialog.loading"
        :option="dialogTableOption" @on-load="loadMemeberData">
      </avue-crud>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.show = false">取 消</el-button>
        <el-button type="primary" @click="dialog.show = false">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  import tableOption from '@/crud/finance/member-recharge-history.js'
  import dialogTableOption from '@/crud/finance/member-recharge-history-orderByMember.js'
  export default {
    data() {
      return {
        showDialog: false,
        amount: 0.00,
        tableOption: tableOption,
        dialogTableOption: dialogTableOption,
        dataList: [],
        dataListLoading: false,
        loading: false,
        searchTimeRange: [],
        searchValue: {
          orderNumber: '',
          startTime: '',
          endTime: '',
          payType: null,
          amount: null,
          nickName: ''
        },
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          size: 10 // 每页显示多少条
        },
        payTypeOptions: [{
          value: 1,
          label: '现金支付'
        }, {
          value: 2,
          label: '银联支付'
        }, ],
        dialog: {
          index: 0,
          show: false,
          loading: false,
          page: null,
          dataList: []
        }
      }
    },

    mounted() {},

    methods: {
      loadData(page) {
        this.getAmount()
        page = (page === undefined ? this.page : page)
        // this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/admin/userDeposit/getUserDeposit'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign({
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.size : page.pageSize,
            }, this.searchValue), false
          )
        }).then(({
          data
        }) => {
          console.log(data)
          this.dataList = data.records
          this.page.total = data.total
          this.dataListLoading = false
        })
      },

      /**
       * 刷新回调
       */

      searchChange(params) {
        if (params.time && params.time.length !== 1) {
          params.startTime = this.dateTimeString(params.time[0]),
            params.endTime = this.dateTimeString(params.time[1])
        }
        delete params.time
        this.loadData(this.page, params)
      },


      handleSearch(state) {
        if (state == 0) {
          this.searchValue = {
              orderNumber: '',
              startTime: '',
              endTime: '',
              payType: null,
              amount: null,
              nickName: ''
            },
            this.loadData()
        } else if (state == 1) {
          var params = {
            orderNumber: this.searchOrderNum,
            startTime: this.searchTimeRange.length !== 0 ? this.dateTimeString(this.searchTimeRange[0]) : '',
            endTime: this.searchTimeRange.length !== 0 ? this.dateTimeString(this.searchTimeRange[1]) : '',
            type: this.searchType
          }
          this.searchValue.startTime = this.searchTimeRange.length !== 0 ? this.dateTimeString(this.searchTimeRange[
            0]) : ''
          this.searchValue.endTime = this.searchTimeRange.length !== 0 ? this.dateTimeString(this.searchTimeRange[1]) :
            ''
          // this.params = params
          console.log(this.searchValue)
          this.loadData()
        }
      },
      //格式化日期时间
      dateTimeString(val) {
        let date = new Date(val)
        let D = [
          String(date.getFullYear()),
          date.getMonth() < 9 ? '0' + (date.getMonth() + 1) : String(date.getMonth() + 1),
          date.getDate() < 10 ? '0' + date.getDate() : String(date.getDate()),
          date.getHours() < 10 ? '0' + date.getHours() : String(date.getHours()),
          date.getMinutes() < 10 ? '0' + date.getMinutes() : String(date.getMinutes()),
          date.getSeconds() < 10 ? '0' + date.getSeconds() : String(date.getSeconds())
        ]
        return D.slice(0, 3).join('-') + ' ' + D.slice(3).join(':')
      },

      //获取当前账户余额
      getAmount() {
        this.$http({
          url: this.$http.adornUrl('/finance/getAccount'),
        }).then(({
          data
        }) => {
          // this.amount = data.account%1 === 0?String(data.account)+'.00':String(data.account)
          console.log(data.account)
          this.amount = (data.account).toFixed(2)
          this.rebateMoneyAmount = data.rebateMoneyAmount
        })
      },

      //提现
      withdraw() {
        if (this.dataForm.cashOutAmount > 0 && this.dataForm.cashOutAmount <= this.amount) {
          this.$http({
            url: this.$http.adornUrl('/cashOut/cashoutByShop'),
            params: this.$http.adornParams({
              cashOutMoney: this.dataForm.cashOutAmount
            })
          }).then(() => {
            this.$message({
              type: 'sucess',
              message: "申请已提交，请等待汇款"
            })
            this.visible = false
            this.getAmount()
          })
        }
      },

      loadMemeberData() {
        let {
          userId,
          page,
          index
        } = this.dialog
        this.$http({
          url: this.$http.adornUrl('/admin/userDeposit/selectDepositMoneyRecord'),
          params: this.$http.adornParams({
            userId,
            ...page
          })
        }).then(({
          data
        }) => {
          this.dialog.loading = false
          this.dialog.dataList = data.records
          page.total = data.total
          page.current = data.current
          page.size = data.size
          this.dataList[index].page = page
        })
      },

      openMemberRechargeHistory(info) {
        console.log(info)
        let {
          index,
          row
        } = info
        this.showDialog = true
        this.dialog.show = true
        this.dialog.loading = true
        let {
          userId
        } = row
        let page = row.page || {
          total: 0,
          current: 1,
          size: 10
        }
        this.dialog.page = page
        this.dialog.userId = userId
        this.dialog.index = index
      },

      handleClose() {
        console.log('handle close')
        this.showDialog = false
      }
    },

  }
</script>
<style lang="scss" scoped>
  // 自定义样式
  .avue-crud__title {
    justify-content: flex-start;
  }

  .el-range-editor--small .el-range-separator {
    width: 30px;
  }

  .avue-crud .avue-crud__left .el-button,
  .avue-crud .avue-crud__right .el-button,
  .avue-form .avue-crud__left .el-button,
  .avue-form .avue-crud__right .el-button {
    margin-right: 8px;
    margin-bottom: 8px;
    height: 34px;
    align-self: flex-end;
  }

  //提现盒子样式
  .Box {
    display: flex;
  }

  .withdrawBox {
    display: flex;
    align-items: center;
    height: 80px;
  }

  .showAmount {
    height: 80px;
    line-height: 80px;
    width: 70%;
  }

  .withdrawBtn {
    height: 40px;
    width: 100px;
    border: 0;
    border-radius: 3px;
    background-color: #02A1E9;
    text-align: center;
    line-height: 40px;
    color: white;
    margin-left: 50px;
    position: relative;
    top: 10px;
  }

  .el-form-item {
    margin-bottom: 5px;
  }
</style>
